package org.sandbox2.domain.bank;

import javax.validation.constraints.NotNull;

import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@RooJavaBean
@RooToString
@RooEntity
public class Account {

	@NotNull
	private String name;

	@NotNull
	private Integer initialAmount;

	private Integer balance;
	
	@Transactional
	public void create() {
		balance = initialAmount;
		this.persist();
	}
	
	@Transactional
//	@Transactional(propagation = Propagation.REQUIRED)
//	@Transactional(propagation = Propagation.MANDATORY)
	public void deposit(Integer amount) {
		System.out.println("Account#deposit");
		balance += amount;
	}
	
	@Transactional
//	@Transactional(propagation = Propagation.REQUIRED)
//	@Transactional(propagation = Propagation.MANDATORY)
	public void withdraw(Integer amount) {
		System.out.println("Account#withdraw");
		if (balance < amount) {
			throw new RuntimeException();
		}
		balance -= amount;
	}
}
